package org.jeecg.glr.job;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.glr.api.constant.ApiConstant;
import org.jeecg.glr.service.IGlrVideoYsService;
import org.jeecg.glr.util.StringUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 海康盒子传过来的数据统一处理
 * 定时任务
 */
@Slf4j
public class HkBoxJob implements Job {

    @Value(value = "${spring.quartz.job.hkbox_is_runing}")
    private String hkbox_is_runing;

    @Autowired
    private IGlrVideoYsService glrVideoYsService;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        long start = System.currentTimeMillis();

        String stJobName="海康盒子数据统计";
        log.info(String.format("1处理"+stJobName+"定时任务开始!===时间:" + DateUtils.getTimestamp()));
        try{
            if (!"Y".equals(hkbox_is_runing.toUpperCase())){
                log.info("2===================处理"+stJobName+"定时任务取消===================");
                return;
            }

            JSONArray jsonArray=new JSONArray();

            synchronized (ApiConstant.HK_BOX_DATA) {
                Iterator<Map.Entry<String, Object>> it = ApiConstant.HK_BOX_DATA.entrySet().iterator();
                while(it.hasNext()){
                    Map.Entry<String, Object> entry = it.next();
                    jsonArray.add(entry);
                    it.remove();
                }
            }

            SimpleDateFormat simpDate = new SimpleDateFormat("yyyy-MM-dd HH");
            String SNapTime=simpDate.format(new Date());
            Map<String, Object> resMap = new HashMap<String, Object>();

            for (int i = 0;i<jsonArray.size();i++){

                Object o=jsonArray.get(i);
                Map.Entry<String, Object> entry11=(Map.Entry<String, Object>)o;
                JSONObject jsonObject = (JSONObject) entry11.getValue();
                Date date= (Date)jsonObject.get("SNapTime");
                SNapTime=simpDate.format(date);
                String stType="";
                if (StringUtil.isNotEmpty(jsonObject.get("type")))stType=jsonObject.get("type").toString();
                String stBySex="";
                if (StringUtil.isNotEmpty(jsonObject.get("BySex")))stBySex=jsonObject.get("BySex").toString();
                String stSerialNumber="";
                if (StringUtil.isNotEmpty(jsonObject.get("SerialNumber")))stSerialNumber=jsonObject.get("SerialNumber").toString();

                //String stType=jsonObject.get("type").toString();
                //String stBySex=jsonObject.get("BySex").toString();
                //String stSerialNumber=jsonObject.get("SerialNumber").toString();

                boolean bfind=false;
                Iterator<Map.Entry<String, Object>> entries = resMap.entrySet().iterator();
                while(entries.hasNext()){
                    Map.Entry<String, Object> entry = entries.next();
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    //System.out.println(key+":"+value);
                    if (key.equals(stSerialNumber)){
                        bfind=true;
                        JSONObject jsonObject11 =(JSONObject)value;
                        jsonObject11.put("SNapTime",SNapTime);
                        if ("human".equals(stType)){
                            if ("0".equals(stBySex)){
                                String typename="human0";
                                int ic=(int)jsonObject11.get(typename);
                                jsonObject11.put(typename,ic+1);
                            }
                            if ("1".equals(stBySex)){
                                String typename="human1";
                                int ic=(int)jsonObject11.get(typename);
                                jsonObject11.put(typename,ic+1);
                            }
                            if ("2".equals(stBySex)){
                                String typename="human2";
                                int ic=(int)jsonObject11.get(typename);
                                jsonObject11.put(typename,ic+1);
                            }
                        }
                        if ("nonmotor".equals(stType)){
                            if ("0".equals(stBySex)){
                                String typename="nonmotor0";
                                int ic=(int)jsonObject11.get(typename);
                                jsonObject11.put(typename,ic+1);

                            }
                            if ("1".equals(stBySex)){

                                String typename="nonmotor1";
                                int ic=(int)jsonObject11.get(typename);
                                jsonObject11.put(typename,ic+1);
                            }
                            if ("2".equals(stBySex)){

                                String typename="nonmotor2";
                                int ic=(int)jsonObject11.get(typename);
                                jsonObject11.put(typename,ic+1);
                            }
                        }
                        if ("motor".equals(stType)){

                            String typename="motor";
                            int ic=(int)jsonObject11.get(typename);
                            jsonObject11.put(typename,ic+1);
                        }

                        entry.setValue(jsonObject11);
                    }
                }

                if (!bfind){
                    JSONObject jsonObject11 =new JSONObject();
                    jsonObject11.put("human0",0);
                    jsonObject11.put("human1",0);
                    jsonObject11.put("human2",0);
                    jsonObject11.put("nonmotor0",0);
                    jsonObject11.put("nonmotor1",0);
                    jsonObject11.put("nonmotor2",0);
                    jsonObject11.put("motor",0);
                    jsonObject11.put("SNapTime",SNapTime);
                    if ("human".equals(stType)){
                        if ("0".equals(stBySex)){
                            jsonObject11.put("human0",1);
                        }
                        if ("1".equals(stBySex)){
                            jsonObject11.put("human1",1);
                        }
                        if ("2".equals(stBySex)){
                            jsonObject11.put("human2",1);
                        }
                    }
                    if ("nonmotor".equals(stType)){
                        if ("0".equals(stBySex)){
                            jsonObject11.put("nonmotor0",1);
                        }
                        if ("1".equals(stBySex)){
                            jsonObject11.put("nonmotor1",1);
                        }
                        if ("2".equals(stBySex)){
                            jsonObject11.put("nonmotor2",1);
                        }
                    }
                    if ("motor".equals(stType)){
                        jsonObject11.put("motor",1);
                    }
                    resMap.put(stSerialNumber,jsonObject11);
                }
            }

            Iterator<Map.Entry<String, Object>> entries = resMap.entrySet().iterator();
            while(entries.hasNext()) {
                Map.Entry<String, Object> entry = entries.next();
                String stSerialNumber = entry.getKey();
                Object value = entry.getValue();
                JSONObject jsonObject11 =(JSONObject)value;
                SNapTime=jsonObject11.get("SNapTime").toString();
                String sqlOne = "select human0,human1,human2,nonmotor0,nonmotor1,nonmotor2,motor \n" +
                        "from glr_video_traffic_monitoring \n" +
                        "where serial_number='"+stSerialNumber+"' and s_nap_time='"+SNapTime+"'";
                List<Map<String, Object>> list = glrVideoYsService.doGetListForJdbc(sqlOne);
                if (list.size()>0){

                    int ihuman0=Integer.parseInt(list.get(0).get("human0").toString()) +Integer.parseInt(jsonObject11.get("human0").toString());
                    int ihuman1=Integer.parseInt(list.get(0).get("human1").toString()) +Integer.parseInt(jsonObject11.get("human1").toString());
                    int ihuman2=Integer.parseInt(list.get(0).get("human2").toString()) +Integer.parseInt(jsonObject11.get("human2").toString());

                    int inonmotor0=Integer.parseInt(list.get(0).get("nonmotor0").toString()) +Integer.parseInt(jsonObject11.get("nonmotor0").toString());
                    int inonmotor1=Integer.parseInt(list.get(0).get("nonmotor1").toString()) +Integer.parseInt(jsonObject11.get("nonmotor1").toString());
                    int inonmotor2=Integer.parseInt(list.get(0).get("nonmotor2").toString()) +Integer.parseInt(jsonObject11.get("nonmotor2").toString());

                    int imotor=Integer.parseInt(list.get(0).get("motor").toString()) +Integer.parseInt(jsonObject11.get("motor").toString());

                    String sql12="update glr_video_traffic_monitoring\n" +
                            "set \n" +
                            "human0="+ ihuman0+",\n" +
                            "human1="+ ihuman1+",\n" +
                            "human2="+ ihuman2+",\n" +
                            "nonmotor0="+inonmotor0+",\n" +
                            "nonmotor1="+inonmotor1+",\n" +
                            "nonmotor2="+inonmotor2+",\n" +
                            "motor="+imotor+"\n" +
                            "where \n" +
                            "serial_number='"+stSerialNumber+"' AND\n" +
                            "s_nap_time='"+SNapTime+"'\n";
                    glrVideoYsService.updateSql(sql12);
                }else{
                    String sql1="INSERT INTO glr_video_traffic_monitoring\n" +
                            "(serial_number,s_nap_time,human0,human1,human2,nonmotor0,nonmotor1,nonmotor2,motor )\n" +
                            "VALUE\n" +
                            "('"+stSerialNumber+"','"+SNapTime+"'," +
                            "'"+jsonObject11.get("human0")+"','"+jsonObject11.get("human1")+"','"+jsonObject11.get("human2")+"'," +
                            "'"+jsonObject11.get("nonmotor0")+"','"+jsonObject11.get("nonmotor1")+"','"+jsonObject11.get("nonmotor2")+"'," +
                            "'"+jsonObject11.get("motor")+"' )";
                    glrVideoYsService.insertSql(sql1);
                }
            }
            log.info("3===================处理"+stJobName+"定时任务结束===================");
        }catch (Exception e){
            e.printStackTrace();
        }
        log.info(String.format("4处理"+stJobName+"定时任务结束!===时间:" + DateUtils.getTimestamp()));
        long end = System.currentTimeMillis();
        long times = end - start;
        log.info("5处理"+stJobName+"4总耗时" + times + "毫秒");
    }

}
